package com.sf.fy.uav.applet.service.impl;

import com.sf.fy.uav.applet.config.DelayQueue;
import com.sf.fy.uav.applet.service.SysNoticeService;
import com.sf.fy.uav.common.enums.ServicePrefixEnum;
import com.sf.fy.uav.common.enums.SettingStatusEnum;
import com.sf.fy.uav.common.request.operate.SysNoticeAddRequest;
import com.sf.fy.uav.common.request.operate.SysNoticeEditRequest;
import com.sf.fy.uav.common.utils.BeanUtil;
import com.sf.fy.uav.common.utils.RedisPrefixKey;
import com.sf.fy.uav.controller.NoticeRest;
import com.sf.fy.uav.entity.Notice;
import com.sf.fy.uav.mapper.NoticeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * @author LTQ
 */
@Service
public class SysNoticeServiceImpl implements SysNoticeService {

    @Autowired
    private NoticeMapper noticeMapper;


    @Autowired
    private NoticeRest noticeRest;

    @Autowired
    private DelayQueue delayQueue;

    @Override
    public void add(SysNoticeAddRequest sysNoticeAddRequest) {
        this.check(sysNoticeAddRequest.getStartTime(), sysNoticeAddRequest.getEndTime(), null);
        Notice notice = BeanUtil.copyProperties(sysNoticeAddRequest, Notice.class);
        notice.setDeleted(false);
        Date startTime = sysNoticeAddRequest.getStartTime();
        Date endTime = sysNoticeAddRequest.getEndTime();
        Date date = new Date();
        if (date.getTime() > startTime.getTime()) {
            throw new RuntimeException("开始时间不能小于当前时间");
        }
        if (startTime.getTime() > endTime.getTime()) {
            throw new RuntimeException("开始时间不能大于等于结束时间");
        }


        if (startTime.getTime() > date.getTime()) {
            //还没开始
            notice.setStatus(SettingStatusEnum.NOT_STARTED.getStatus());
            noticeMapper.insert(notice);
            //设置到期自动上架
            delayQueue.pushQueue(ServicePrefixEnum.EXPRESS_SYSTEM + RedisPrefixKey.NOTICE_UP_COUNT_DOWN + notice.getId(), startTime.getTime() - date.getTime());
        } else {
            if (date.getTime() < endTime.getTime()) {
                notice.setStatus(SettingStatusEnum.ON_DISPLAY.getStatus());
                noticeMapper.insert(notice);
                //设置到期自动下架
                delayQueue.pushQueue(ServicePrefixEnum.EXPRESS_SYSTEM + RedisPrefixKey.NOTICE_DOWN_COUNT_DOWN + notice.getId(), endTime.getTime() - date.getTime());
            } else {
                notice.setStatus(SettingStatusEnum.ENDED.getStatus());
                noticeMapper.insert(notice);
            }
        }
    }

    private void check(Date startTime, Date endTime, String id) {
        List<Notice> notices = noticeRest.selectNotStartAndIng(startTime, endTime, id);
        if (!CollectionUtils.isEmpty(notices)) {
            throw new RuntimeException("当前时间段内已存在公告，请重新设置。");
        }
    }


    @Override
    public void edit(SysNoticeEditRequest sysNoticeEditRequest) {
        this.check(sysNoticeEditRequest.getStartTime(), sysNoticeEditRequest.getEndTime(), sysNoticeEditRequest.getId());
        Notice notice = noticeMapper.selectById(sysNoticeEditRequest.getId());
        if (ObjectUtils.isEmpty(notice)) {
            throw new RuntimeException("notice不存在");
        }
        Date startTime = sysNoticeEditRequest.getStartTime();
        Date endTime = sysNoticeEditRequest.getEndTime();
        Date date = new Date();
        if (!SettingStatusEnum.ON_DISPLAY.getStatus().equals(sysNoticeEditRequest.getStatus())) {
            if (date.getTime() > startTime.getTime()) {
                throw new RuntimeException("开始时间不能小于当前时间");
            }
        }
        if (startTime.getTime() > endTime.getTime()) {
            throw new RuntimeException("开始时间不能大于等于结束时间");
        }
        if (StringUtils.isBlank(sysNoticeEditRequest.getProcessedBy())) {
            throw new RuntimeException("处理人不能为空");
        }
        notice.setProcessedBy(sysNoticeEditRequest.getProcessedBy());
        notice.setName(sysNoticeEditRequest.getName());
        notice.setContent(sysNoticeEditRequest.getContent());
        notice.setStartTime(startTime);
        notice.setEndTime(endTime);
        notice.setUpdateTime(date);
        notice.setSort(sysNoticeEditRequest.getSort());
        if (startTime.getTime() > date.getTime()) {
            //还没开始
            notice.setStatus(SettingStatusEnum.NOT_STARTED.getStatus());
            //设置自动上架
            delayQueue.pushQueue(ServicePrefixEnum.EXPRESS_SYSTEM + RedisPrefixKey.NOTICE_UP_COUNT_DOWN + notice.getId(), startTime.getTime() - date.getTime());
        } else {
            if (date.getTime() < endTime.getTime()) {
                notice.setStatus(SettingStatusEnum.ON_DISPLAY.getStatus());
                //设置自动下架
                delayQueue.pushQueue(ServicePrefixEnum.EXPRESS_SYSTEM + RedisPrefixKey.NOTICE_DOWN_COUNT_DOWN + notice.getId(), endTime.getTime() - date.getTime());
            } else {
                notice.setStatus(SettingStatusEnum.ENDED.getStatus());
            }
        }
        noticeMapper.updateById(notice);
    }
}
